package com.imcode;

import java.util.*;

public class PokerV2 {
    public static void main(String[] args) {
        //使用双列Map(HashMap)集合，完成一个数字与字符串纸牌的对应关系(相当于一个字典)。

        // 1.生成扑克牌
        // 1.0 存放扑克牌的双列集合
        Map<Integer, String> pokerMap = new HashMap<>();

        // 1.1 存放花色的集合
        List<String> colors = new ArrayList<>();
        Collections.addAll(colors, "♥", "♦", "♠", "♣");
        // 1.2 存放牌面的集合
        List<String> numbers = new ArrayList<>();
        Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");

        // 1.3 存入大王和小王
        int num = 1;
        pokerMap.put(num++, "大王");
        pokerMap.put(num++, "小王");

        // 1.4 存入其它牌面
        for (String number : numbers) {
            for (String color : colors) {
                pokerMap.put(num++, color + number);
            }
        }

        System.out.println("总共的牌数:" + pokerMap.size());
        // 2.洗牌
        // 2.1 将pokerMap中所有的key 放入到一个ArrayList集合中
        List<Integer> keys = new ArrayList<>();

        for (Integer key : pokerMap.keySet()) {
            keys.add(key);
            System.out.println(key + "-->" + pokerMap.get(key));
        }
        // 2.2 打乱key的顺序
        Collections.shuffle(keys);

        // 3.发牌
        // 3.1 创建三个玩家的集合
        Map<Integer, String> play1 = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return -o1.compareTo(o2);
            }
        });
        Map<Integer, String> play2 = new TreeMap<>();
        Map<Integer, String> play3 = new TreeMap<>();
        Map<Integer, String> dipai = new TreeMap<>();

        // 3.2 遍历打乱循环后的keys的集合来完成发牌
        int i = 0;
        for (Integer key : keys) {
            //底牌
            if (i > 50) {
                dipai.put(key, pokerMap.get(key));
            }
            // 给三个玩家发牌
            else {
                if (i % 3 == 0) {
                    play1.put(key, pokerMap.get(key));
                } else if (i % 3 == 1) {
                    play2.put(key, pokerMap.get(key));
                } else {
                    play3.put(key, pokerMap.get(key));
                }
            }
            i++;
        }

        //4.看牌
        System.out.print("玩家一\t");
        for(Integer key : play1.keySet()){
            System.out.print(play1.get(key) + "\t");
        }

        System.out.println();

        System.out.print("玩家二\t");
        for(Integer key : play2.keySet()){
            System.out.print(play2.get(key) + "\t");
        }

        System.out.println();
        System.out.print("玩家三\t");
        for(Integer key : play3.keySet()){
            System.out.print(play3.get(key) + "\t");
        }

        System.out.println();
        System.out.print("看底牌\t");
        for(Integer key : dipai.keySet()){
            System.out.print(dipai.get(key) + "\t");
        }
    }
}
